package com.renjun.smweb.service.CommonService.impl;


import com.renjun.smweb.commons.componet.BaseDaoComponent;
import com.renjun.smweb.commons.enums.EStandTables;
import com.renjun.smweb.commons.global.DataDict;
import com.renjun.smweb.commons.util.Format;
import com.renjun.smweb.dao.bean.Parameter;
import com.renjun.smweb.dao.bean.QueryParams;
import com.renjun.smweb.dao.pojo.LoginInfo;
import com.renjun.smweb.exceptions.BaseException;
import com.renjun.smweb.service.CommonService.CommonService;
import com.renjun.smweb.commons.componet.BaseDaoComponent;
import com.renjun.smweb.commons.util.Format;
import com.renjun.smweb.dao.bean.Parameter;
import com.renjun.smweb.dao.pojo.LoginInfo;
import com.renjun.smweb.exceptions.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公共方法 接口类 实现
 * Created by 向立军 on 2017/3/27.
 */
@Service
public class CommonServiceImpl implements CommonService {

    @Autowired
    private BaseDaoComponent baseDaoComponent;

    @Override
    public boolean checkDate(Map<String, Object> params) throws SQLException, BaseException, ParseException {
        LoginInfo loginInfo = (LoginInfo) params.get("userinfo");
        //       OPENTYPE 1为登记报名,2录取,3生成录取通知书,4配置区县报名个数
        String openType = (String) params.remove("opentype");
        String openName = "";
        if ("1".equals(openType)) {
            openName = "登记报名";
        } else if ("2".equals(openType)) {
            openName = "录取";
        } else if ("3".equals(openType)) {
            openName = "生成录取通知书";
        }
        boolean flag = false;
        String schoolgrade = loginInfo.getBxdj();
        String xxbb = loginInfo.getXxbb();
        QueryParams qeuryOpenList = QueryParams.createQueryParams("t_open_configtime");
        qeuryOpenList.addQueryParams(Parameter.createParameter("opentype", openType));
        qeuryOpenList.addQueryParams(Parameter.createParameter("districtcode", loginInfo.getDistrictcode()));
        qeuryOpenList.addQueryParams(Parameter.createParameter("schoolgrade", schoolgrade));
        qeuryOpenList.addQueryParams(Parameter.createParameter("xxbb", xxbb));
        try {
            qeuryOpenList.setDbLog(loginInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Map<String, Object>> openList = baseDaoComponent.selectDataByParams(qeuryOpenList);
        if (openList.size() > 0) {
            String verification = (String) openList.get(0).get("verification");
            String starttime = (String) openList.get(0).get("starttime");
            String starttime2 = starttime.substring(0, 4) + "-" + starttime.substring(4, 6) + "-" + starttime.substring(6, 8);
            String endtime = (String) openList.get(0).get("endtime");
            String endtime2 = starttime.substring(0, 4) + "-" + endtime.substring(4, 6) + "-" + endtime.substring(6, 8);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            Date startDate = sdf.parse(starttime);
            Date endDate = sdf.parse(endtime);
            Date now = new Date();
//            if ("1".equals(verification)) {//未打开时间段 打开不打开没有关系 2017年3月27日17:06:54
            if (endDate.after(now) || endDate.equals(now)) {
                if (startDate.before(now) || startDate.before(now)) {
                    flag = true;
                } else {
                    throw new BaseException(DataDict.getChineseValueByCode(EStandTables.区县表, loginInfo.getDistrictcode()) + DataDict.getChineseValueByCode(EStandTables.学校办别表, xxbb) + DataDict.getChineseValueByCode(EStandTables.办学等级表, schoolgrade) + "学校" + openName + "通道时间为" + starttime2 + "至" + endtime2 + ",无法进行此操作");
                }
            } else {
                throw new BaseException(DataDict.getChineseValueByCode(EStandTables.区县表, loginInfo.getDistrictcode()) + DataDict.getChineseValueByCode(EStandTables.学校办别表, xxbb) + DataDict.getChineseValueByCode(EStandTables.办学等级表, schoolgrade) + "学校" + openName + "通道时间为" + starttime2 + "至" + endtime2 + ",无法进行此操作");
            }
//            } else {
//                throw new BaseException(DataDict.getChineseValueByCode(EStandTables.区县表, loginInfo.getDistrictcode()) + DataDict.getChineseValueByCode(EStandTables.学校办别表, xxbb) + DataDict.getChineseValueByCode(EStandTables.办学等级表, schoolgrade) + "学校" + openName + "通道处于关闭状态");
//            }
        } else {
            throw new BaseException(DataDict.getChineseValueByCode(EStandTables.区县表, loginInfo.getDistrictcode()) + DataDict.getChineseValueByCode(EStandTables.学校办别表, xxbb) + DataDict.getChineseValueByCode(EStandTables.办学等级表, schoolgrade) + "学校" + openName + "通道处于未开启状态");
        }
        return flag;
    }

    @Override
    public Map<String, Object> getCheckDateTime(Map<String, Object> params) throws SQLException {
        LoginInfo loginInfo = (LoginInfo) params.get("userinfo");
//       OPENTYPE 1为登记报名,2录取,3生成录取通知书,4配置区县报名个数
        String openType = (String) params.remove("opentype");
        QueryParams queryDate = QueryParams.createQueryParams("T_OPEN_CONFIGTIME");//查询时间段
        queryDate.addQueryParams(Parameter.createParameter("opentype", openType),
        Parameter.createParameter("districtcode", loginInfo.getDistrictcode()));//区县
        if ("1".equals(openType) || "2".equals(openType) || "3".equals(openType)) {
            queryDate.addQueryParams(Parameter.createParameter("schoolgrade", loginInfo.getBxdj()),//办学等级
            Parameter.createParameter("xxbb", loginInfo.getXxbb()));//办学登记
        }
        Map<String, Object> dateMap = baseDaoComponent.selectOneDataByParams(queryDate);
        String startTime = (String) dateMap.get("starttime");
        String endTime = (String) dateMap.get("endtime");

        Map<String, Object> timeMap = new HashMap<>();
        timeMap.put("starttime", startTime);
        timeMap.put("endtime", endTime);
        //校验时间段
        String time = Format.getDateNumbers();
        Integer startDate = Integer.parseInt(startTime);
        Integer endDate = Integer.parseInt(endTime);
        Integer timeInt = Integer.parseInt(time);

        //在时间段范围内，返回false，不在时间范围内返回true
        if (startDate <= timeInt && timeInt <= endDate) {
            timeMap.put("time", "1");//1.代表在范围内
        } else {
            timeMap.put("time", "0");//0.代表不在范围内
        }
        return timeMap;
    }
}
